package utilities.event;

import hibernate.HibernateInitializer;
import hibernate.clientAccount.SingleClient;
import hibernate.hibernateInterface.ClientSystemHibernateInterface;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.api.*;

import utilities.event.SearchEvents.RestrictionCheck;
import utilities.utilities_components.PlainList;
import utilities.utilities_components.SumUp;

public class ACSListener implements EventListener{
		
	public ACSListener(){}
		
	public void onEvent( Event event ){
		((PlainList)event.getPage().getAttribute("list")).update(
																getData(
																		event.getPage().getAttribute("name").toString(),
																		event.getPage().getAttribute("ageCheck").equals(true),
																		((Integer)event.getPage().getAttribute("low_age")),
																		((Integer)event.getPage().getAttribute("high_age")),
																		(Date)event.getPage().getAttribute("timeFrom"),
																		(Date)event.getPage().getAttribute("timeTo"),
																		((Integer)event.getPage().getAttribute("sex")),
																		(Comboitem)event.getPage().getAttribute("searchRes"),
																		(Comboitem)event.getPage().getAttribute("roomRes")
																 		)
																 );
	}
	
	private List getData(String name,boolean ageChecked,Integer low_age,Integer high_age,Date timeFrom,Date timeTo,Integer sex,Comboitem searchRes,Comboitem roomRes){
		LinkedList ret = new LinkedList();
		LinkedList resultList = new LinkedList();
		
		Session session = HibernateInitializer.getSession();
		if (searchRes != null && (searchRes.getContent().equals("房号") && roomRes != null))
		{
			SingleClient sc ;
			try {
				 sc = ClientSystemHibernateInterface.getSingleClientsByRoomNumber(roomRes.getContent(), session);
				 if (sc == null)
					 return ret;
			}catch(Exception e){return null;}

			LinkedList col = new LinkedList();
			if (sc.getPersonalInfo() == null)
			{
				col.add("");
				col.add(roomRes.getContent());
				col.add(sc.getCurrentRoom().getRoom().getRoomType());
				for (int i = 0; i < 6;i++)
					col.add("");
				col.add(sc.getIsGroupMember() ? sc.getGroup() : "");
			}
			else
			{
				col.add(sc.getPersonalInfo().getFirstName());
				col.add(roomRes.getContent());
				col.add(sc.getCurrentRoom().getRoom().getRoomType());
				col.add(sc.getPersonalInfo().getSex() ? "男" : "女");
				col.add(sc.getPersonalInfo().getBirthday());
				col.add(sc.getPersonalInfo().getDocType());
				col.add(sc.getPersonalInfo().getDocNumber());
				col.add(sc.getPersonalInfo().getConcreteAddress().getCityAddress());
				col.add(sc.getPersonalInfo().getRegisterDate());
				col.add(sc.getIsGroupMember() ? sc.getGroup() : "");
			}
			col.add(sc.getNote());
			ret.add(col);
		}
		else 
		{
			try {
				resultList.addAll(ClientSystemHibernateInterface.getAllSingleClients(session));
			}catch(Exception e){return null;}
			if (resultList.size() == 0)
				return ret;
			else
				for (int i = 0 ;i < resultList.size();i++)
				{
					SingleClient sc = (SingleClient)resultList.get(i);
					if (sc == null)
						continue;
					if (!check(sc,name,ageChecked,low_age,high_age,timeFrom,timeTo,sex,searchRes,roomRes))
						continue;
					LinkedList col = new LinkedList();
					if (sc.getPersonalInfo() == null)
					{
						col.add("");
						col.add(sc.getCurrentRoom().getRoom().getRoomNumber());
						col.add(sc.getCurrentRoom().getRoom().getRoomType());
						for (int j = 0; j < 6;j++)
						{
							col.add("");
						}
						col.add(sc.getIsGroupMember() ? sc.getGroup() : "");
					}	
					else
					{
						col.add(sc.getPersonalInfo().getFirstName());
						col.add(sc.getCurrentRoom().getRoom().getRoomNumber());
						col.add(this.parse(sc.getCurrentRoom().getRoom().getRoomType().toString()));
						col.add(sc.getPersonalInfo().getSex() ? "男" : "女");
						col.add(sc.getPersonalInfo().getBirthday());
						col.add(sc.getPersonalInfo().getDocType());
						col.add(sc.getPersonalInfo().getDocNumber());
						col.add(sc.getPersonalInfo().getConcreteAddress().getCityAddress());
						col.add(sc.getPersonalInfo().getRegisterDate());
						col.add(sc.getIsGroupMember() ? sc.getGroup() : "");
					}
					col.add(sc.getNote());
					ret.add(col);
				}
		}
		session.close();
		return ret;
	}
	
	private static String parse(String roomType){
		if(roomType.equals("SINGLE"))
			return "单人间";
		if(roomType.equals("DOUBLE"))
			return "双人间";
		if(roomType.equals("TRIPLE"))
			return "三人间";
		if(roomType.equals("QUAD"))
			return "四人间";
		if(roomType.equals("SUITE"))
			return "套间";
		if(roomType.equals("TSU"))
			return "总统套房";
			return "标准间";
	}
	
	private boolean check(SingleClient sc,String name,boolean ageChecked,Integer low_age,Integer high_age,
			Date timeFrom,Date timeTo,int sexRes,Comboitem searchRes,Comboitem roomRes)
	{
		if (sc.getPersonalInfo() == null)
			return true;

		if (name != null && !name.equals("")) 
			if (!RestrictionCheck.nameCheck(name, sc.getPersonalInfo().getFirstName(), sc.getPersonalInfo().getMiddleName(), sc.getPersonalInfo().getLastName()))
				return false;

		if (ageChecked)
		{
			if (RestrictionCheck.ageCheck(sc.getPersonalInfo().getAge(),low_age,high_age))
				;
			else
				return false;
		}

		Date birthday = sc.getPersonalInfo().getBirthday();
		if (birthday != null && !RestrictionCheck.dateCheck(birthday, timeFrom, timeTo))
			return false;

		boolean sex = sc.getPersonalInfo().getSex();
		if (!RestrictionCheck.sexCheck(sex, sexRes))
			return false;

		if (searchRes != null)
			if (roomRes != null)
				if (searchRes.getContent().equals("房型") && !parse(sc.getCurrentRoom().getRoom().getRoomType().toString()).equals(roomRes.getContent()))
					return false;
		return true;
	}
}
